﻿using Common;
using STMS.BLL;
using STMS.Models.UIModels;
using STMS.WSTMSystem.FModels;
using STMS.WSTMSystem.UControls;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace STMS.WSTMSystem.StoreTemper
{
    public partial class FrmStoreTemperatureList : Form
    {
        public FrmStoreTemperatureList()
        {
            InitializeComponent();
        }

        RegionTemperBLL regionTemperBLL = new RegionTemperBLL();
        private void FrmStoreTemperatureList_Load(object sender, EventArgs e)
        {
            cboStores.LoadStores();

            LoadStoreRegionsTemperList();//加载仓库分区温度监控数据列表


        }

        private void LoadStoreRegionsTemperList()
        {
            int storeId = cboStores.SelectedValue.GetInt();
            List<UCStoreBoxModel> boxList = regionTemperBLL.GetStoreBoxList(storeId, -1);
            ShowBoxList(boxList);

        }

        private void ShowBoxList(List<UCStoreBoxModel> boxList)
        {
            if (boxList.Count > 0)
            {
                flpList.Controls.Clear();
                foreach (UCStoreBoxModel uc in boxList)
                {
                    UCStoreBoxNew box = new UCStoreBoxNew();
                    box.UCStoreSource = uc;
                    if (uc.TemperState != 1)
                        AddSetTemperatureEvent(box, uc.TemperState);
                    box.Margin = new Padding(10, 20,10,20);
                    flpList.Controls.Add(box);
                }
            }
        }

        /// <summary>
        /// 订阅升温或降温事件
        /// </summary>
        /// <param name="box"></param>
        /// <param name="state"></param>
        private void AddSetTemperatureEvent(UCStoreBoxNew box, int state)
        {
            if (state == 0)
            {
                box.SetTemperature += UpTemperature;
            }
            else
                box.SetTemperature += DownTemperature;
        }



        /// <summary>
        /// 降温
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void DownTemperature(object sender, UCBoxArgs e)
        {
            FrmSetSRegionTemper fSetTemper = new FrmSetSRegionTemper();
            int regionId = e.RegionId;
            decimal srTemper = e.SRTemperature;
            int state = e.TemperState;
            fSetTemper.Tag = new RegionTemperData()
            {
                RegionId = regionId,
                SRTemper = srTemper,
                TemperState = state,
                SId = e.SId
            };
            fSetTemper.ReloadRegion += ReloadRegion;
            fSetTemper.Show();
        }



        /// <summary>
        /// 升温
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void UpTemperature(object sender, UCBoxArgs e)
        {
            FrmSetSRegionTemper fSetTemper = new FrmSetSRegionTemper();
            int regionId = e.RegionId;
            decimal srTemper = e.SRTemperature;
            int state = e.TemperState;
            fSetTemper.Tag = new RegionTemperData()
            {
                RegionId = regionId,
                SRTemper = srTemper,
                TemperState = state,
                SId = e.SId
            };
            fSetTemper.ReloadRegion += ReloadRegion;
            fSetTemper.Show();
        }

        private void ReloadRegion(UCBoxArgs e)
        {
            foreach (Control c in flpList.Controls)
            {
                if (c is UCStoreBoxNew)
                {
                    UCStoreBoxNew box = c as UCStoreBoxNew;
                    if (box.SRegionId == e.RegionId)
                    {
                        box.SRTemper = e.SRTemperature;
                        box.TemperState = e.TemperState;
                        break;
                    }
                }
            }
        }

        /// <summary>
        /// 手动检测
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnManualCheck_Click(object sender, EventArgs e)
        {
            //读取仓库温度数据并修改入库

            Task.Run(() =>
            {
                this.Invoke(new Action(() => {
                    lblMsg.Text = "开始检测，请稍后。。。";
                }));
                Thread.Sleep(2000);
                try
                {
                    this.Invoke(new Action(() => {
                        List<UCStoreBoxModel> boxList = regionTemperBLL.ReadRegionTemperatures();
                        if (boxList != null&&boxList.Count >0)
                        {
                            ShowBoxList(boxList);
                            lblMsg.Text = "检测完成！";
                        } 
                        else
                        {
                            lblMsg.Text = "检测失败，请检查是否连接设备！";
                            lblMsg.ForeColor = Color.Red;
                            return;
                        }
                       
                    }));
                }
               catch(Exception ex)
                {
                    MessageBox.Show("检测异常！");
                }
            });
        }

        private void cboStores_SelectedIndexChanged(object sender, EventArgs e)
        {
            LoadStoreRegionsTemperList();
        }

        private void btnRefresh_Click(object sender, EventArgs e)
        {
            FrmStoreTemperatureList_Load(this, e);
        }
    }
}
